<template>
  <div class="goods-image">
    <div ref="el" class="middle">
      <img :src="images[currIndex]" alt="" />
      <div class="large"  v-show="!isOutside" :style="`background:url(${images[currIndex]}) no-repeat;background-position:${poslayer.X*-2 + 'px'} ${poslayer.Y*-2 + 'px'}`">

      </div>
      <div
        class="layer"
        v-show="!isOutside"
        :style="{ left: poslayer.X + 'px', top: poslayer.Y + 'px' }"
      ></div>
    </div>
    <ul class="small">
      <li
        v-for="(img, i) in images"
        :key="img"
        :class="{ active: i === currIndex }"
      >
        <img @mouseenter="currIndex = i" :src="img" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
import { ref, watch, reactive } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    images: {
      type: Array,
      default: () => []
    }
  },
  setup (props) {
    const currIndex = ref(0)
    const el = ref(null)
    const { isOutside, elementX, elementY } = useMouseInElement(el)
    const poslayer = reactive({
      X: 0,
      Y: 0
    })

    watch([elementY, elementX], () => {
      poslayer.X = elementX.value - 100
      poslayer.Y = elementY.value - 100
      if (poslayer.X < 0) poslayer.X = 0
      if (poslayer.X > 200) poslayer.X = 200
      if (poslayer.Y < 0) poslayer.Y = 0
      if (poslayer.Y > 200) poslayer.Y = 200
    })
    return { currIndex, el, isOutside, poslayer }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .middle {
    width: 480px;
    height: 400px;
    cursor:zoom-in;
    position: relative;
    display: flex;
    z-index: 500;
    .layer {
      height: 200px;
      width: 200px;
      background: rgba(0, 0, 0, 0.5);
      position: absolute;
      top: 0;
      left: 0;
    }
    .large {
      position: absolute;
      top: 0;
      left: 412px;
      width: 400px;
      height: 400px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      background-repeat: no-repeat;
      background-size: 800px 800px!important;
      background-color: #f8f8f8;
      overflow: hidden;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
